package com.dachuang.canjiren.service.impl;

import com.dachuang.canjiren.entity.Banner;
import com.dachuang.canjiren.mapper.BannerMapper;
import com.dachuang.canjiren.service.IBannerService;
import com.dachuang.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BannerServiceImpl implements IBannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public R insertBanner(String img, String url, String title) {

        if (StringUtils.isEmpty(img)){
            return R.error(1,"img不能为空");
        }

        Banner banner = new Banner();
        banner.setImg(img);
        banner.setTitle(title);
        banner.setUrl(url);

        int i = bannerMapper.insertBanner(banner);
        if (i > 0){
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R selectList() {

       List<Banner> list = bannerMapper.selectList();
       Map<String,Object> map = new HashMap<>();
        if (list.size() > 0){
            map.put("data",list);
            return R.ok(map);
        }
        return R.error();
    }


}
